﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ShopModel;

namespace ShopDAO
{
    public class PartnersDAO
    {
        private string connectionString;

        private SqlConnection Connection()
        {
            return new SqlConnection(connectionString);
        }

        public PartnersDAO()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csShop"].ConnectionString;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="onlyActive"></param>
        /// <returns>Zwraca liste obiektow klasy Partner</returns>
        public List<Partner> GetPartners(bool onlyActive)
        {
            SqlConnection sqlC = new SqlConnection(connectionString);
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;
            command.CommandText = "GetPartners";
            command.CommandType = CommandType.StoredProcedure;

            List<Partner> result = new List<Partner>();
            command.Parameters.Add(new SqlParameter("@onlyActive", onlyActive));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                while (sqlR.Read())
                {
                    result.Add(new Partner((int)sqlR["IDPartner"], sqlR["Name"] is DBNull ? string.Empty : (string)sqlR["Name"], (int)sqlR["Weight"], (bool)sqlR["IsActive"], (string)sqlR["PartnersUrl"]));
                }
                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return null;
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="partner"></param>
        /// <returns></returns>
        public bool SavePartner(Partner partner)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = "SavePartner";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlCommand.Parameters.Add(new SqlParameter("@IDPartner", partner.PartnerID));
            sqlCommand.Parameters.Add(new SqlParameter("@Name", partner.Name));
            sqlCommand.Parameters.Add(new SqlParameter("@Weight", partner.Weight));
            sqlCommand.Parameters.Add(new SqlParameter("@IsActive", partner.IsActive));
            sqlCommand.Parameters.Add(new SqlParameter("@PartnersUrl", partner.PartnersUrl));

            try
            {
                connection.Open();
                return sqlCommand.ExecuteNonQuery() > 0;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PartnerId"></param>
        /// <returns></returns>
        public bool DeletePartner(int PartnerId)
        {

            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = "DeletePartner";
            sqlCommand.CommandType = CommandType.StoredProcedure;

            sqlCommand.Parameters.Add(new SqlParameter("@PartnerId", PartnerId));

            try
            {
                connection.Open();
                return (int)sqlCommand.ExecuteScalar() > 0;
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return false;
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PartnerId"></param>
        /// <returns>Zwraca obiekt klasy Partner</returns>
        public Partner GetPartner(int PartnerId)
        {
            SqlConnection sqlC = new SqlConnection(connectionString);
            SqlCommand command = sqlC.CreateCommand();
            SqlDataReader sqlR;

            Partner result = null;

            command.CommandText = "GetPartner";
            command.CommandType = CommandType.StoredProcedure;

            command.Parameters.Add(new SqlParameter("@PartnerId", PartnerId));

            try
            {
                sqlC.Open();
                sqlR = command.ExecuteReader();

                if (sqlR.Read())
                    result = new Partner((int)sqlR["IDPartner"], sqlR["Name"] is DBNull ? string.Empty : (string)sqlR["Name"],  (int)sqlR["Weight"], (bool)sqlR["IsActive"], (string)sqlR["PartnersUrl"]);

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
                return null;
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }
    }
}
